import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import moment from 'moment';
import { Row, Col, Card, Form, Input, Select, Icon, Button, Dropdown, Menu, InputNumber, DatePicker, Modal, message, Badge, Divider, Steps, Radio, Popconfirm } from 'antd';
import BaseComponent from '@/components/BaseComponent';
import MyIcon from '@/components/MyIcon';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import StandardTable from '@/components/StandardTable';
import StandardTableHandle from '@/components/StandardTable/StandardTableHandle';
import { miniApps as tabList } from '@/commons/tabList';
import { feedbacks as columns } from '@/commons/columns';

const FormItem = Form.Item;
const { Step } = Steps;
const { TextArea } = Input;
const { Option } = Select;
const getValue = obj =>
  Object.keys(obj)
  .map(key => obj[key])
  .join(',');

/* eslint react/no-multi-comp:0 */
@connect(({ feedbacks, loading }) => ({
  feedbacks,
  loading: loading.models.feedbacks,
}))
@Form.create()
class TableList extends BaseComponent {
  constructor(props) {
    super(props);
    this.namespace = 'feedbacks';
    this.title = '问题反馈';
  }

  columns = [...columns, {
    title: '操作',
    width: 99,
    align: 'center',
    fixed: 'right',
    render: ({ id, status }) => (
      <Popconfirm title="确认标记为已审阅?" disabled={status} onConfirm={this.handleUpdate.bind(null, id)} okText="确认" cancelText="取消">
        <Button icon="edit" type="primary" size="small" disabled={status}>{status ? '已审阅' : '未审阅'}</Button>
      </Popconfirm>
    ),
  }];

  handleUpdate = id => {
    const { dispatch } = this.props;

    dispatch({
      type: 'feedbacks/update',
      payload: { id },
      callback: ({ errno, data }) => {
        if (errno) {
          message.error('已审阅失败！');
        } else {
          dispatch({
            type: 'feedbacks/fetch',
          });

          message.success(`序号${id}的问题反馈已审阅成功~`);
        }
      }
    });
  }

  render() {
    const {
      feedbacks: { data, type, status },
      loading,
    } = this.props;
    const { selectedRows } = this.state;

    return (
      <PageHeaderWrapper 
        title={this.title} 
        action={moment().format('LLLL')}
        tabList={tabList}
        tabActiveKey={type}
        onTabChange={this.handleTabChange}
      >
        <Card bordered={false}>
          <StandardTableHandle
            status={status}
            selectedRows={selectedRows}
            radioGroups={{
              'all': <Fragment><Icon type="appstore" />&nbsp;全部</Fragment>,              
              '0': <Fragment><MyIcon type="icon-review" />&nbsp;待审阅</Fragment>,
              '1': <Fragment><MyIcon type="icon-preview" />&nbsp;已审阅</Fragment>,
            }}
            menuItems={{
              'remove': <Button type="danger" icon="delete" size="small" style={{width: '100%'}}>删除</Button>,
              'update': <Button icon="edit" size="small" style={{width: '100%'}}>审阅</Button>
            }}
            action={<a href="/favicon.png" target="_blank"><Button type="primary" icon="download">EXCEL</Button></a>}
            onToggleTable={this.handleGroupChange}
            onMenuClick={this.handleMenuClick}
          />
          <StandardTable
            selectedRows={selectedRows}
            loading={loading}
            data={data}
            scroll={{ x: 1300 }}
            columns={this.columns}
            onSelectRow={this.handleSelectRows}
            onChange={this.handleTableChange}
          />
        </Card>
      </PageHeaderWrapper>
    );
  }
}

export default TableList;